package com.survey.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.survey.common.utils.DateUtils;
import com.survey.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.survey.system.mapper.SurSchoolMapper;
import com.survey.system.domain.SurSchool;
import com.survey.system.service.ISurSchoolService;

/**
 * 学校信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class SurSchoolServiceImpl implements ISurSchoolService 
{
    @Autowired
    private SurSchoolMapper surSchoolMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询学校信息
     * 
     * @param schoolId 学校信息主键
     * @return 学校信息
     */
    @Override
    public SurSchool selectSurSchoolBySchoolId(Long schoolId)
    {
        return surSchoolMapper.selectSurSchoolBySchoolId(schoolId);
    }

    /**
     * 查询学校信息列表
     * 
     * @param surSchool 学校信息
     * @return 学校信息
     */
    @Override
    public List<SurSchool> selectSurSchoolList(SurSchool surSchool)
    {
        List<SurSchool> surSchools = surSchoolMapper.selectSurSchoolList(surSchool);
        for (SurSchool school : surSchools) {
            String sysSchoolType = sysDictDataMapper.selectDictLabel("sys_school_type", school.getSchoolType().toString());
            school.setSchoolTypeName(sysSchoolType);
        }
        return surSchools;
    }

    /**
     * 新增学校信息
     * 
     * @param surSchool 学校信息
     * @return 结果
     */
    @Override
    public int insertSurSchool(SurSchool surSchool)
    {
        surSchool.setCreateTime(DateUtils.getNowDate());
        return surSchoolMapper.insertSurSchool(surSchool);
    }

    /**
     * 修改学校信息
     * 
     * @param surSchool 学校信息
     * @return 结果
     */
    @Override
    public int updateSurSchool(SurSchool surSchool)
    {
        surSchool.setUpdateTime(DateUtils.getNowDate());
        return surSchoolMapper.updateSurSchool(surSchool);
    }

    /**
     * 批量删除学校信息
     * 
     * @param schoolIds 需要删除的学校信息主键
     * @return 结果
     */
    @Override
    public int deleteSurSchoolBySchoolIds(Long[] schoolIds)
    {
        return surSchoolMapper.deleteSurSchoolBySchoolIds(schoolIds);
    }

    /**
     * 删除学校信息信息
     * 
     * @param schoolId 学校信息主键
     * @return 结果
     */
    @Override
    public int deleteSurSchoolBySchoolId(Long schoolId)
    {
        return surSchoolMapper.deleteSurSchoolBySchoolId(schoolId);
    }

    @Override
    public List<Integer> countByDate() {
        List<Integer> numList = new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 获取今天的数据（今天的日期格式化为字符串）
        String today = dateFormat.format(date);
        int countToday = surSchoolMapper.countByDate(today);
        numList.add(countToday);  // 添加今天的数据

        // 获取过去6天的数据
        for (int i = 1; i <= 6; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);  // 每次回退1天
            Date time = calendar.getTime();
            String formattedDate = dateFormat.format(time);
            int count = surSchoolMapper.countByDate(formattedDate);
            numList.add(count);
        }

        return numList;
    }


}
